package com.ljh.localmusic.runtime

import com.ljh.localmusic.data.repository.AiRepository
import com.ljh.localmusic.ui.model.ImageUrlModel
import com.ljh.localmusic.utils.ext.logd
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
import javax.inject.Inject

/**
 * author：LJH on 2025/9/17 19:26
 *  获取Ai图片
 */
class GenerateImageUsecase @Inject constructor(
    private val aiRepository: AiRepository
) {

    suspend operator fun invoke() : ImageUrlModel = withContext(Dispatchers.IO){
        try {
            val result = aiRepository.generateImage()
            val url = result.data?.firstOrNull()?.url ?: ""
            val errorMsg = result.error?.message ?: ""
            val createTime = result.created ?: 0
            "获取到的图片地址为: $url，errorMsg: $errorMsg，createTime: $createTime".logd()
            ImageUrlModel(
                url = url,
                errorMsg = errorMsg,
                createTime = createTime
            )
        } catch (e: Exception){
            e.printStackTrace()
            ImageUrlModel()
        }
    }
}